package com.kuqi.presenters;

import android.content.Context;
import android.os.Handler;
import androidx.lifecycle.Lifecycle;
import com.kuqi.core.ApplicationPersistent;
import com.kuqi.entitiy.BannerItem;
import com.kuqi.entitiy.HomePageData;
import com.kuqi.entitiy.VideoItem;
import java.util.ArrayList;
import java.util.List;


public class HomePagePresenter extends BasePresenter {

    // 上下文
    private Context context;
    // 生命周期
    private Lifecycle lifecycle;
    // 命令回调函数
    private Handler callBackHandler;
    public static final int SEARCH_DATA_FINISH = 10001;

    //几个相关联的数据列表
    private List<BannerItem> bannerList;  // 轮播图显示数据
    private List<VideoItem> cartonList;  // 卡通模块
    private List<VideoItem> musicList;  // 音乐模块
    private List<VideoItem> scienceList;  // 科技模块

    public HomePagePresenter(Context context, Lifecycle lifecycle, Handler callBackHandler) {
        super(context, lifecycle);
        this.context = context;
        this.lifecycle = lifecycle;
        this.callBackHandler = callBackHandler;
    }

    public List<BannerItem> getBannerList(){
        return bannerList;
    }
    public List<VideoItem> getCartonList(){
        return cartonList;
    }
    public List<VideoItem> getMusicList(){
        return musicList;
    }
    public List<VideoItem> getScienceList(){
        return scienceList;
    }


    /**
     * 获取轮播图图像路径列表
     * @return list of pictures
     */
    public ArrayList<String> getBannerPictureList(){
        ArrayList<String> picturePaths = new ArrayList<>();
        for (BannerItem item :bannerList) {
            picturePaths.add(item.getPicture());
        }
        return picturePaths;
    }


    /**
     * 调用方法查询基础显示需要的数据
     * 由于功能迁移，预加载已经放到splash阶段
     */
    public void searchBaseData(){
        // 直接去持久层获取
        HomePageData data = ApplicationPersistent.getApplication().getHomePageData();
        bannerList = data.getBannerList();
        cartonList = data.getCartonList();
        musicList = data.getMusicList();
        scienceList = data.getScienceList();
        // 查询执行完成，触发回调通知
        callBackHandler.sendMessage(callBackHandler.obtainMessage(SEARCH_DATA_FINISH));
    }

}
